package name.jiaosha.air.system.service.impl;

import name.jiaosha.air.system.dao.IAreaDao;
import name.jiaosha.air.system.dao.IBrandDao;
import name.jiaosha.air.system.dao.ICategoryDao;
import name.jiaosha.air.system.dao.IFunctionDao;
import name.jiaosha.air.system.dao.IPowerDao;
import name.jiaosha.air.system.dao.ISeerDao;
import name.jiaosha.air.system.dao.IUnitDao;
import name.jiaosha.air.system.domain.Brand;
import name.jiaosha.air.system.domain.Category;
import name.jiaosha.air.system.domain.Power;
import name.jiaosha.air.system.domain.Seer;
import name.jiaosha.air.system.domain.SysFunction;
import name.jiaosha.air.system.domain.Unit;
import name.jiaosha.air.system.service.ISystemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

@Service
public class SystemService implements ISystemService {

	@Autowired
	private IUnitDao unitDao;
	@Autowired
	private IAreaDao areaDao;
	@Autowired
	private IBrandDao brandDao;
	@Autowired
	private IPowerDao powerDao;
	@Autowired
	private IFunctionDao functionDao;
	@Autowired
	private ISeerDao seerDao;
	
	@Autowired
	private ICategoryDao categoryDao;

	@Override
	public void createUnit(Unit unit) {

		unit.setPinyin(PinyinToolkit.cn2FirstSpell(unit.getName()));
		unitDao.save(unit);
	}

	@Override
	public Unit getUnitById(Integer id) {

		return unitDao.get(id);
	}

	@Override
	public PageInfo<Unit> getUnitListByComId(int index, int pageSize) {

		String jpql = "SELECT u FROM Unit u";

		return unitDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updateUnit(Unit unit) {
		unit.setPinyin(PinyinToolkit.cn2FirstSpell(unit.getName()));
		unitDao.saveOrUpdate(unit);

	}

	@Override
	public void createBrand(Brand brand) {

		brand.setPinyin(PinyinToolkit.cn2FirstSpell(brand.getName()));
		brandDao.save(brand);
	}

	@Override
	public Brand getBrandById(Integer id) {

		return brandDao.get(id);
	}

	@Override
	public PageInfo<Brand> getBrandListByComId(int index, int pageSize) {

		String jpql = "SELECT b FROM Brand b";

		return brandDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updateBrand(Brand brand) {

		brand.setPinyin(PinyinToolkit.cn2FirstSpell(brand.getName()));
		brandDao.saveOrUpdate(brand);

	}

	@Override
	public void createPower(Power power) {

		powerDao.save(power);

	}

	@Override
	public Power getPowerById(Integer id) {

		return powerDao.get(id);
	}

	@Override
	public PageInfo<Power> getPowerListByComId(int index, int pageSize) {

		String jpql = "SELECT p FROM Power p";

		return powerDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updatePower(Power power) {

		powerDao.saveOrUpdate(power);
	}

	@Override
	public void createFunction(SysFunction sysFunction) {

		sysFunction.setPinyin(PinyinToolkit.cn2FirstSpell(sysFunction.getName()));
		functionDao.save(sysFunction);
	}

	@Override
	public void createSeer(Seer seer) {

		seerDao.save(seer);
	}

	@Override
	public SysFunction getFunctionById(Integer id) {

		return functionDao.get(id);
	}

	@Override
	public PageInfo<SysFunction> getFunctionListByComId(int index, int pageSize) {

		String jpql = "SELECT f FROM SysFunction f";

		return functionDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public Seer getSeerById(Integer id) {

		return seerDao.get(id);
	}

	@Override
	public PageInfo<Seer> getSeerListByComId(int index, int pageSize) {

		String jpql = "SELECT p FROM Seer p";

		return seerDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updateFunction(SysFunction sysFunction) {
		sysFunction.setPinyin(PinyinToolkit.cn2FirstSpell(sysFunction.getName()));
		functionDao.saveOrUpdate(sysFunction);

	}

	@Override
	public void updateSeer(Seer seer) {

		seerDao.saveOrUpdate(seer);

	}

	@Override
	public void createCategory(Category category) {
		category.setPinyin(PinyinToolkit.cn2FirstSpell(category.getName()));
		categoryDao.save(category);
	}

	@Override
	public Category getCategoryById(Integer id) {
		
		return categoryDao.get(id);
	}

	@Override
	public PageInfo<Category> getCategoryList(int index, int pageSize) {
		
		String jpql = "SELECT c FROM Category c";

		return categoryDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updateCategory(Category category) {
		category.setPinyin(PinyinToolkit.cn2FirstSpell(category.getName()));
		categoryDao.saveOrUpdate(category);
		
	}
	
	

}
